package com.lp.module_home_c.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;

import com.lp.module_home_c.R;
import com.lp.module_home_c.R2;
import com.lp.module_home_c.bean.TextBean;
import com.lp.module_home_c.dialog.JokeDialogUtils;
import com.lp.module_home_c.dialog.TextDialogUtils;
import com.lp.module_home_c.presenter.HomeCPresenter;
import com.lp.module_lib.base.BaseActivity;
import com.lp.module_lib.network.mvp.BaseModel;
import com.lp.module_lib.view.AppTitleBar;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;


/**
 * 作者：created by LP_TREAM on 2020/11/25 22:28
 * 邮箱：806875201@qq.com
 */
public class HomeCActivity extends BaseActivity<HomeCPresenter> implements HomeCPresenter.HomeCView {

    @BindView(R2.id.appTitleBar)
    AppTitleBar mAppTitleBar;
    @BindView(R2.id.tv_get_text)
    TextView mTvGetText;
    @BindView(R2.id.tv_upload_image)
    TextView mTvUploadImage;
    @BindView(R2.id.tv_change_base_url)
    TextView mTvChangeBaseUrl;
    @BindView(R2.id.tv_down_file)
    TextView mTvDownFile;

    @Override
    protected HomeCPresenter createPresenter() {
        return new HomeCPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.home_c_activity_home;
    }

    @Override
    protected void initData() {
        mAppTitleBar.setTitleName("网络框架请求案例");
        mAppTitleBar.setTitleColor(getResources().getColor(R.color.colorWhite));
    }

    /**
     * 获取网络数据
     *
     * @param o
     */
    @Override
    public void onTextSuccess(BaseModel<TextBean> o) {
        TextDialogUtils.showDialog(mActivity, o.getResult().getData(), null);
    }

    /**
     * 多baserUrl测试
     *
     * @param o
     */
    @Override
    public void onJokeListSuccess(BaseModel<List<String>> o) {
        JokeDialogUtils.showDialog(mActivity, o.getJokes());
    }

    /**
     * 图片上传
     *
     * @param o
     */
    @Override
    public void onUploadSuccess(BaseModel<Object> o) {
        showToast(o.getMsg());
    }

    @Override
    public void onDownFileSuccess(File file) {
        showToast("文件下载成功 保存路径：" + file.getAbsolutePath());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R2.id.tv_get_text, R2.id.tv_change_base_url, R2.id.tv_upload_image, R2.id.tv_upload_file, R2.id.tv_down_file})
    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.tv_get_text) {
            mPresenter.getText();
        } else if (id == R.id.tv_change_base_url) {
            mPresenter.getJokeList("https://autumnfish.cn/");
        } else if (id == R.id.tv_upload_image) {
            upLoadImage();
        } else if (id == R.id.tv_upload_file) {
            upLoadFile();
        } else if (id == R.id.tv_down_file) {
            getPermission();
        }
    }

    /**
     * 图片上传演示
     */
    private void upLoadImage() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .maxSelectNum(1)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    /**
     * 图片上传演示
     */
    private void upLoadFile() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofVideo())
                .maxSelectNum(1)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @SuppressLint("CheckResult")
    private void getPermission() {
        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE
        ).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(@NonNull Boolean granted) throws Exception {
                if (granted) {
                    String url = "https://bjlzbt.com/upload/default//20190725//c13948258c6ef6a36cbe2d3322b98f5c.mp4";
                    mPresenter.downFile(url, Environment.getExternalStorageDirectory().getAbsolutePath(), "ceshi.mp4");
                } else {
                    showToast("部分权限未获取!!");
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    if (selectList.size() > 0) {
                        if (selectList.get(0).getPath().contains(".mp4")) {
                            mPresenter.getUploadOne(selectList.get(0).getPath());
                        } else {
                            mPresenter.getUpload(selectList);
                        }
                    }
                    break;

            }
        }
    }
}
